<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<html>
<head>
<%
       try
       {
		if(session.getAttribute("logged_in") == "true")
		{
			
			if(session.getAttribute("user_level") == "administrator")
			{
				String DRIVER = "com.mysql.jdbc.Driver";
				Class.forName(DRIVER);
				Connection con=null;
				ResultSet rst=null;

				String url="jdbc:mysql://ecstiger.cs.andrews.edu/d562_2010_01?user=u562_2010_01&password=YPJ8f4We";
				con=DriverManager.getConnection(url);

				PreparedStatement stmt = con.prepareStatement("SELECT presentation.file_name, user.id, user.username FROM presentation, user WHERE presentation.user_id = user.id AND presentation.id = '" + request.getParameter("id") + "';");
				
				rst = stmt.executeQuery();

				if(rst.next())
				{
					File file = new File("/var/lib/tomcat6/webapps/cptr562-2010-01/home/bravogon/websignage/files/" + rst.getString(3) +"/" + rst.getString(1));

					// Get the number of bytes in the file
					int length = (int)file.length();

					response.setContentType("application/force-download");
					//response.setHeader("Content-Transfer-Encoding", "Binary");
					//response.setHeader("Content-length", ""+length);
					response.setHeader("Content-disposition", "attachment; filename=" + file.getName());
					//response.setHeader("Filename", file.getName());				
					FileInputStream fstream = new FileInputStream("/var/lib/tomcat6/webapps/cptr562-2010-01/home/bravogon/websignage/files/" + rst.getString(3) +"/" + rst.getString(1));
					// Get the object of DataInputStream
					DataInputStream in = new DataInputStream(fstream);
					BufferedReader br = new BufferedReader(new InputStreamReader(in));

					String strLine;
					//Read File Line By Line
					while ((strLine = br.readLine()) != null)   
					{
						// Print the content on the console
      						out.println (strLine);
					}
			
				}
				else
				{
					out.println("Error");
				}


				

				

				rst.close();
				stmt.close();
				con.close();




			}
			else
			{
				
				 out.println("<br><h3>You selected presentation user_id: " + request.getParameter("user_id") + "</h3><br><br>"); 

	                        String DRIVER = "com.mysql.jdbc.Driver";
				Class.forName(DRIVER);
				Connection con=null;
				ResultSet rst=null;

				String url="jdbc:mysql://ecstiger.cs.andrews.edu/d562_2010_01?user=u562_2010_01&password=YPJ8f4We";
				con=DriverManager.getConnection(url);

				PreparedStatement stmt = con.prepareStatement("SELECT * FROM presentation WHERE id = '" + request.getParameter("id") + "' AND user_id='" + session.getAttribute("id") + "';");
				
				rst = stmt.executeQuery();
				
				if(rst.next())
				{
					File file = new File("/var/lib/tomcat6/webapps/cptr562-2010-01/home/bravogon/websignage/files/" + rst.getString(3) +"/" + rst.getString(1));

					// Get the number of bytes in the file
					int length = (int)file.length();

					response.setContentType("application/force-download");
					//response.setHeader("Content-Transfer-Encoding", "Binary");
					//response.setHeader("Content-length", ""+length);
					response.setHeader("Content-disposition", "attachment; filename=" + file.getName());
					//response.setHeader("Filename", file.getName());				
					FileInputStream fstream = new FileInputStream("/var/lib/tomcat6/webapps/cptr562-2010-01/home/bravogon/websignage/files/" + rst.getString(3) +"/" + rst.getString(1));
					// Get the object of DataInputStream
					DataInputStream in = new DataInputStream(fstream);
					BufferedReader br = new BufferedReader(new InputStreamReader(in));

					String strLine;
					//Read File Line By Line
					while ((strLine = br.readLine()) != null)   
					{
						// Print the content on the console
      						out.println (strLine);
					}
			
				}
				else
				{
					out.println("Error");
				}

                                rst.close();
				stmt.close();
				con.close();
			}


		}
		else
		{
			out.println("Not Logged In");
		}
       }
       catch(Exception e)
       {
           out.println(e);
       }	
%>
   <title></title>
</head>

<br><a href="menu.jsp">Main Menu</a>
</center>
<body></body>
</html>
